<?php

require_once './include/common.inc.php';
checkLogin();
$postiondiv='<div class="position"><a href="index.php">Home</a> &gt; <span class="position">View Wish List</span></div>';
$baner_img='<div class="bigtitle">Wish List</div>';

$page_title = 'jans2en furniture - Wish List - Wish List Management';
getMyParam('pid');
getMyParam('wid');
getMyParam('wname');
getMyParam('act');
getMyParam('code');
getMyParam('new');

$from_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $PHP_SELF;

if ($act == "add" && is_numeric($pid) && $pid>0) {
	$sql = "SELECT `id`, `product_list` FROM `wishlist` WHERE `user_id` = $jansen_uid AND `name`='$wname'";
	$tmp = $db->get_one($sql);
	if (!empty($tmp)) {
		echo "<script>alert('Wish list name \'$wname\' exists!');</script>";
		exit;
	}
	
    $sql = "SELECT * FROM `product` WHERE `id` = $pid";
    $product_info_ary = $db->get_one($sql);
    
	if($new)
	{
		$sql = "INSERT INTO `wishlist` (product_list, date, user_id, flag,name) VALUES ($pid, $timestamp, $jansen_uid, 0 , '$wname')";
		$db->query($sql);
	}
	else
	{
		$sql = "SELECT `id`, `product_list` FROM `wishlist` WHERE `user_id` = $jansen_uid AND `id`=$wid ORDER BY date DESC";
		$tmp = $db->get_one($sql);
		if (!empty($tmp)) {
			if (!in_array($pid, explode("|", $tmp['product_list']))) {
				$new_product_list = $tmp['product_list'] == "" ? $pid : $tmp['product_list'] . "|" . $pid;
				$sql = "UPDATE `wishlist` SET `product_list`= '$new_product_list' WHERE `id` = " . $wid;
				$db->query($sql);
			}
		} else {
			$sql = "INSERT INTO `wishlist` (product_list, date, user_id, flag,name) VALUES ($pid, $timestamp, $jansen_uid, 0 ,$wname)";
			$db->query($sql);
		}
	}
    
    $sql = "SELECT `id` FROM `wishlist` WHERE `user_id` = $jansen_uid";
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);
    setcookie('jansen_wishlist_qty', $sql_all_num, time()+3600*24*365, "/");

    echo "<script>alert('Item $code has been added to Wish List.');parent.location.href='" . $from_url . "';</script>";
    exit;
    //header("Location:wishlist_view.php");
} elseif ($act == "delall" && is_numeric($wid) && $wid>0) {
    $sql1 = "DELETE FROM `wishlist` WHERE `id` = $wid AND `user_id` = '$jansen_uid'";
    $sql2 = "DELETE FROM `sendrecord` WHERE `wishlist_id` = $wid AND `user_id` = '$jansen_uid'";
    $db->query($sql1);
    $db->query($sql2);
    header("Location:wishlist_view.php");
}
elseif ($act == "edit" && is_numeric($wid) && $wid>0) {
	
	$sql1 = "SELECT COUNT(id) as num FROM `wishlist` WHERE `name` = '$wname' AND `user_id` = '$jansen_uid'";
	$rs1 = $db->get_one($sql1);
	
	if($rs1["num"] > 0) 
	{
		echo "<script>alert('Wish list name \'$wname\' exists!');</script>";
	}
	else
	{
		$sql1 = "UPDATE `wishlist` SET `name`='".$wname."' WHERE `id` = $wid AND `user_id` = '$jansen_uid'";
		$db->query($sql1);
		echo "<script>alert('Update success!".$rs1[0]."');parent.location.href='" . $from_url . "';</script>";
	}
	exit;
}


// *** page code start ***
$page_url = 'wishlist_view.php?wid=' . $wid . '&';
$sql = "SELECT `id` FROM `wishlist` WHERE `user_id` = $jansen_uid";
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);
$s = $sql_all_num > 1 ? "s":""; //if add 's' to string 'list'.

$ppp = 10;
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}

$page_show = getPhotoPageStr($sql_all_num, $page, $ppp, $page_url, "wish lists");
// *** page code end ***

$sql = "SELECT `id`, `product_list`, `date`, `name` FROM `wishlist` WHERE `user_id` = $jansen_uid ORDER BY `date` DESC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $wishlist_ary[] = $rs;
}

//echo $sql_all_num;
setcookie('jansen_wishlist_qty', $sql_all_num, time()+3600*24*365, "/");



$sql = "SELECT `id`, `wishlist_id`, `date`, `friend_name`, `friend_email` FROM `sendrecord` WHERE `user_id` = $jansen_uid";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $sendrecord_ary[$rs['wishlist_id']][] = $rs;
}

require_once PrintEot('header');
require_once PrintEot('wishlist_view');
require_once PrintEot('footer');

?>